/* beautify ignore:start */
import {Component, Input, OnInit, Output, EventEmitter, SimpleChange} from '@angular/core';
import {isPresent} from '../common';
/* beautify ignore:end */

@Component({
    selector: 'easy-pagination',  
    styleUrls: ['easy-pagination.component.css'],
    templateUrl: 'easy-pagination.component.html'
})
export class EasyPaginationComponent { 
    public _tableData: any;  //显示数据
    public _dataClone: any;  //克隆数据 
    private current: number = 1;  //当前页
    private pageSize: number = 3;  //页面记录数
    private pages: number;  //总页面数
    private total: number;  //总记录数 
     
    @Input() 
    set tableData(value: any){
        if (value) {
            this._tableData = value;      
        }  
    };  
    
    get tableData() {
        return this._tableData;
    }
    
    @Input() 
    set dataClone(value: any){
        if (value) {
            this._dataClone = value; 
            this.initSet(); 
            this.initData();
            this.changeData();          
        }  
    }; 
    
    get dataClone() {
        return this._dataClone;  
    }
    
  

    // 初始化
    ngOnInit() {
       this.initSet();       
    } 

    //初始化表格配置
    initSet() {
        this.current =    1;
        this.pageSize =   3;    
    }
    
    //数据
    initData() {
        if(this._dataClone) { 
            this.total =   this._dataClone.length;
            this.pages =   this.getPages(); 
        } 
    }
    
 
    // 计算最多页数
    private getPages(): number {
        let totalPages = this.total< 1 ? 1 : Math.ceil(this.total / this.pageSize);
        return totalPages;
    }
    
    //更新数据
    changeData() {    
       this._tableData = this._dataClone.slice((this.current-1)*this.pageSize,(this.current-1)*this.pageSize+this.pageSize); 
    } 
    
    //上一页、下一页 
    selectPage(page: any) {
        this.current = page;
        this.changeData(); 
    }



    // 是否有上一页
    private noPrevious(): boolean {
        return this.current === 1;
    }

    // 是否下一页
    private noNext(): boolean {
        return this.current === this.pages;
    }


}